import $ from 'jquery'

// 域名
export const domain = 'https://www.baidu.com'

/*
* 功能：懒加载
* 参数：
*     data： 接口获得的数据
*     mountData: 需要渲染的数据
*     obj: 组件实例对象
*     firstCount: 首次加载的条数
*     preCount： 后来每次加载的条数
* */
export function lazyLoad (data, mountData, obj, firstCount, preCount) {
  let timeCount = 0
  let totalCount = data.length // 后台获得的数据的总长度
  let mountCounting = 0 // 需要展示的条数
  firstCount = firstCount || 10
  preCount = preCount || 5

  // 如果数据太少 不进行懒加载
  if (totalCount <= firstCount) {
    obj.mountData = data
    console.log('数据太少，不需要懒加载')
    return
  }
  mountData = data.slice(0, firstCount)

  obj.mountData = mountData

  $(window).on('scroll', function () {
    let _scroll = document.documentElement.scrollTop || document.body.scrollTop || window.scrollY || window.pageYOffset
    let bodyHeight = $('body').innerHeight() || document.body.scrollHeight || document.body.offsetHeight || document.body.clientHeight
    let screenHeight = $(window).innerHeight()
    if (_scroll >= bodyHeight - screenHeight - 80) {
      timeCount++
      mountCounting = firstCount + preCount * timeCount
      if (mountCounting > totalCount) {
        mountCounting = totalCount
        timeCount--
        // 已经拉倒了最底部 下面处理提示操作
        obj.noMoreData = true
      }
      mountData = data.slice(0, mountCounting)
    }
    obj.mountData = mountData
  })
}

/*
* 没有更多提示功能
* 结合懒加载lazyLoad函数使用 要求在Vue组件中实例对象具有noMoreData属性 用来标记是否还有数据可以加载
* */
export function nomoreData (obj) {
  let startX, startY, moveX, moveY, distanceX, distanceY, touch
  let liHeight = $(obj.$refs.noMore).siblings('li').height()
  $(obj.$refs.fileList).on('touchstart', function (e) {
    if (e.targetTouches.length !== 1) { return false }
    touch = e.targetTouches[0]
    startX = touch.clientX
    startY = touch.clientY
  })
  $(obj.$refs.fileList).on('touchmove', function (e) {
    if (e.targetTouches.length !== 1) { return false }
    touch = e.targetTouches[0]
    moveX = touch.clientX
    moveY = touch.clientY
    distanceX = moveX - startX
    distanceY = startY - moveY
    if (Math.abs(distanceX) > Math.abs(distanceY)) { return false }
    distanceY = distanceY.toFixed(0)
    if (distanceY < 0) { distanceY = 0 }
    if (distanceY > liHeight) { distanceY = liHeight }
    $(obj.$refs.noMore).height(obj.noMoreData ? distanceY : 0)
  })
  $(obj.$refs.fileList).on('touchend', function () {
    distanceY = 0
    if (obj.noMoreData) {
      $(obj.$refs.noMore).animate({height: 0}, 500)
    }
  })
}

/*
*  文件列表下拉和上滑操作
* */
export function pullDown (e, obj) {
  let _self = e.target
  let boxHeight = $(_self).siblings('.file_sel').height()

  // 如果用户将一个文件划上去之后去划另一个 重置属性使得未操作的文件都是划下来的状态
  if (_self !== obj.eTarget) {
    obj.isPull = false
  }

  obj.isPull = !obj.isPull
  if (obj.isPull) { // 上划的时候做的事情
    // 排他 先将所有的文件都划下来
    $('.pulldown').removeClass('ispulldown')
    $('.file_sel').animate({top: boxHeight}, 200)

    // 将正在操作的文件划上去
    $(_self).siblings('.file_sel').animate({top: 0}, 200)
  } else { // 下拉的时候做的事情
    // 下拉动画
    $(_self).siblings('.file_sel').animate({top: boxHeight}, 200)
  }

  // 箭头改变方向
  $(_self).toggleClass('ispulldown')
  obj.eTarget = _self
}

// 根据文件类型对应不同的图像
export function handleDocType (obj) {
  let titleArr, titleType

  obj.falseData.forEach(v => {
    titleArr = v.title.split('.')
    titleType = titleArr.pop()
    v.imgUrl = obj.imgUrl[titleType] || obj.imgUrl.unknowUrl
  })
}

/*
* 判断用户手机平台
* */
export function handleMobileType () {
  let uA = navigator.userAgent
  let mobileType // 1-->ios  2-->Android  3-->weixin

  if (uA.match(/(iPhone|iPad|iPod|iOS)/i)) {
    mobileType = 1
  } else if (/android/i.test(uA)) {
    mobileType = 2
  } else if (uA.indexOf('MicroMessenger') > -1) {
    mobileType = 3
  }

  return mobileType
}

/*
* 格式化时间
* */
export function formatTime (time, hour) {
  if (typeof time !== 'number') {
    throw new Error('\'time\' must be a number')
  }

  let hours, minutes, seconds, timeStr
  time = parseInt(time)

  hours = Math.floor(time / 3600)
  minutes = Math.floor((time % 3600) / 60)
  seconds = (time % 60)

  hours = hours < 10 ? '0' + hours : hours
  minutes = minutes < 10 ? '0' + minutes : minutes
  seconds = seconds < 10 ? '0' + seconds : seconds
  timeStr = hour ? hours + ':' + minutes + ':' + seconds : minutes + ':' + seconds

  return timeStr
}

/*
* 表单验证之发送验证码
* */
export function sendCode (obj) {
  if (!obj.canSendCode) { return false }

  let timer, i

  i = 59
  obj.canSendCode = false
  obj.sendCodeMessage = i + 's后重发'

  timer = setInterval(function () {
    i--
    if (i === 0) {
      obj.sendCodeMessage = '发送验证码'
      obj.canSendCode = true
      clearInterval(timer)
    } else {
      obj.sendCodeMessage = i + 's后重发'
    }
  }, 1000)
}

/*
* 表单验证之密码
* */
export function checkoutPassword (obj) {
  let psw = obj.psw1.val

  obj.psw1.isFocus = false

  if (!psw) {
    obj.psw1.warningMessage = '密码不能为空'
  } else if (psw.length > 16 || psw.length < 8) {
    obj.psw1.warningMessage = '请输入8~16位密码'
  } else if (/[^A-Za-z0-9_~@#$\\.]/.test(psw)) {
    obj.psw1.warningMessage = '密码不支持_~@#.以外的特殊符号或中文字符'
  } else {
    obj.psw1.isRight = true
    return false
  }

  obj.psw1.isRight = false
}
